package com.jzvd;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import java.util.LinkedHashMap;

import cn.jzvd.JZVideoPlayerStandard;

/**
 * 这里可以监听到视频播放的生命周期和播放状态
 * 所有关于视频的逻辑都应该写在这里
 * Created by Nathen on 2017/7/2.
 */
public class MyJZVideoPlayerStandard extends JZVideoPlayerStandard {
    public MyJZVideoPlayerStandard(Context context) {
        super(context);
    }

    public MyJZVideoPlayerStandard(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void init(Context context) {
        super.init(context);
        findViewById(cn.jzvd.R.id.fullscreen).setVisibility(GONE);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_custom_jz_play;
    }


    private JZMediaPlayerListener callback;
    public void setJZMediaPlayerListener(JZMediaPlayerListener callback) {
        this.callback = callback;
    }

    @Override
    public void setProgressAndText(int progress, long position, long duration) {
        super.setProgressAndText(progress, position, duration);
        if (callback != null) {
            callback.onProgressUpdate(progress, position, duration);
        }
    }

    @Override
    public void startVideo() {
        super.startVideo();
        Log.d("TAG", "startVideo");
    }

    @Override
    public void onStateNormal() {
        super.onStateNormal();
    }

    @Override
    public void onStatePrepared() {
        super.onStatePrepared();
        if (callback != null) {
            callback.onPrepared();
        }
    }

    @Override
    public void onStatePlaying() {
        super.onStatePlaying();
    }

    @Override
    public void onStatePause() {
        super.onStatePause();
    }

    @Override
    public void onStateError() {
        super.onStateError();
    }

    @Override
    public void onStateAutoComplete() {
        super.onStateAutoComplete();
        if (callback != null) {
            callback.onStateAutoComplete();
        }
    }

    @Override
    public void onInfo(int what, int extra) {
        super.onInfo(what, extra);
        if (callback != null) {
            if (what == JZMediaPlayerListener.MEDIA_INFO_VIDEO_RENDERING_START) {
                callback.onPrepared();
            } else if (what == JZMediaPlayerListener.MEDIA_INFO_BUFFERING_END) {
                loadingProgressBar.setVisibility(GONE);
                callback.onBufferingEnd();
            } else if (what == JZMediaPlayerListener.MEDIA_INFO_BUFFERING_START) {
                callback.onBufferingStart();
                loadingProgressBar.setVisibility(VISIBLE);
            } else {
                callback.onInfo(what, extra);
            }
        }
    }

    @Override
    public void onError(int what, int extra) {
        super.onError(what, extra);
        if (callback != null) {
            callback.onError(what, extra);
        }
    }

    @Override
    public void startWindowFullscreen() {
        super.startWindowFullscreen();
    }

    @Override
    public void startWindowTiny() {
        super.startWindowTiny();
    }

    @Override
    public void onCompletion() {
        super.onCompletion();
        if (callback != null) {
            callback.onCompletion();
        }
    }

    public void startLocalPlay(String filePath) {
        LinkedHashMap map = new LinkedHashMap();
        map.put("URL_KEY_DEFAULT", filePath);
        dataSourceObjects[0] = map;
        onStatePreparingChangingUrl(0, getCurrentPositionWhenPlaying());
    }


    public void setOnDownLoadClickListener(OnClickListener downloadClickListener) {
        findViewById(R.id.download).setOnClickListener(downloadClickListener);
    }
}
